$(function(){
		/*初始化表格*/
		$('#dg').datagrid({
    		url:'backbidinvitejson',
    		collapsible:true,
    		fit:true,
			fitColumns:true,
			toolbar:'#footer',
			pagination:true,
			pageSize:20,
			pageList:[20,50,100,200],
			remoteSort:false,
			onLoadSuccess:function(data){
		        if (data.total == 0) { 
				//colspan为实际显示的总列数，appendRow、mergeCells不用修改
				$(this).datagrid('appendRow', { inviteName: '<div style="text-align:center;color:red">没有相关记录！</div>' }).datagrid('mergeCells', { index: 0, field: 'inviteName', colspan: 10 }) 
				//隐藏分页导航条
				$(this).closest('div.datagrid-wrap').find('div.datagrid-pager').hide(); 
				//隐藏选择框
				 $(this).datagrid('hideColumn', 'ck');
				} 
				//有数据时显示选择框、分页导航条
				else {
					$(this).closest('div.datagrid-wrap').find('div.datagrid-pager').show();
					$(this).datagrid('showColumn', 'ck');}
		    },
   			columns:[[
   			 	{field:'ck',checkbox:true},
   			 	{field:'inviteID',hidden:'true'},
				{field:'inviteName',title:'招标名称',align:'center',width:'10%',
				formatter:function(value,row,index){
					if(row.inviteID == undefined)return row.inviteName;
				
					return '<a href="bidinviteinfo?iInviteID='+row.inviteID+'">'+row.inviteName+'</a>'
				}},
				{field:'type',title:'招标类型',align:'center',width:'8%',formatter: function(value,row,index){
	        	if(value == 0)
	        		return '公务';
	        	else if(value == 1)
	        		return '团建';
				else if(value == 2)
	        		return '业内';
				else
				return '私密游';
	        }},
				{field:'zoneName',title:'招标目的地',align:'center',width:'12%'},
				{field:'name',title:'联系人',align:'center',width:'10%'},
				{field:'tel',title:'联系方式',align:'center',width:'10%'},
				{field:'beginDate',title:'招标开始日期',align:'center',width:'10%',formatter:function(value) {
            var date = new Date(value);
            var year = date.getFullYear().toString();
            var month = (date.getMonth() + 1);
            var day = date.getDate().toString();
            if (month < 10) {
                month = "0" + month;
            }
            if (day < 10) {
                day = "0" + day;
            }
            return year + "-" + month + "-" + day;
        }},
				{field:'endDate',title:'招标结束日期',align:'center',width:'10%',formatter:function(value) {
            var date = new Date(value);
            var year = date.getFullYear().toString();
            var month = (date.getMonth() + 1);
            var day = date.getDate().toString();
            if (month < 10) {
                month = "0" + month;
            }
            if (day < 10) {
                day = "0" + day;
            }
            return year + "-" + month + "-" + day;
        }},
				{field:'budget',title:'预算金额',align:'center',width:'10%'},
				{field:'count',title:'参投数量',align:'center',width:'8%',formatter: function(value,row,index){
	        	if(value == "" || value==null)
	        		return '0';
	       
				else
				return value;
	        }},
				{field:'state',title:'发布状态',align:'center',width:'10%',formatter: function(value,row,index){
	        	if(value == 0)
	        		return '未发布';
	        	else if(value == 1)
	        		return '您的总团款低于2000元,不能发布';
				else if(value == 2)
	        		return '等待审核,请及时缴纳保证金';
				else if(value == 3)
	        		return '已发布';
	            else if(value == 4)
	        		return '审核不通过，请及时修改';
			
				else
				return '已结束';
	        }}
    		]]
		});
			
});

/*搜索*/
function searchInvite(){
	
		var inviteName = $("#searchInviteName").val();
		var type = $("#searchType").val();
		
		$("#dg").datagrid("load",{
			inviteName:inviteName,
			type:type
		})
	}
	
/*清空*/
function clearInvite(){
        $("#searchInviteName").val('');
		$('#searchType').val('');
	
     }
//后台	招标新建与修改时正则验证 2020.11.19
function inviteSubmit(inviteForm){
	
if(inviteForm.Province.value==""){ 
  $.messager.alert('错误','请选择所在省！','error');
	inviteForm.Province.focus();
	return false;	
    } 
if(inviteForm.City.value==""){ 
	$.messager.alert('错误','请选择所在市！','error');
	inviteForm.City.focus();
	return false;	
    }
if(inviteForm.ZoneID.value==""){ 
		$.messager.alert('错误','请选择所在区！','error');
    
	inviteForm.ZoneID.focus();
	return false;	
    }
if(!(/^1[3456789]\d{9}$/.test(inviteForm.Tel.value))){ 
		$.messager.alert('错误','联系人手机号格式错误！','error');
 
	inviteForm.Tel.focus();
	return false;	
    } 
	}
// 修改招标基本信息 hanxl 2020.11.19
function updateBidInvite(){
	var rows = $('#dg').datagrid('getSelections');
		var checkNum = rows.length;
		if(checkNum==0){
			$.messager.alert('错误','至少选择一项','error');
		 } else if(checkNum>1){
			$.messager.alert('错误','最多选择一项','error');
		 }else{
			
			var state = rows[0].state;
			if(state==0 ||state==1 ||state==4){
		$.messager.confirm('Confirm', '确定要修改吗?', function(r){
	if (r){
		var iInviteID = rows[0].inviteID; 
		 	location.href="/mmy/toupdatebidinvite?iInviteID="+iInviteID;
	}
});
			
	
	}else{
		$.messager.alert('警告','你已通过审核或已发布或招标已结束，不能修改','warning');
		
	}
			
			
		 }

}

/*发布*/
//2020.12.03 hanxl 后台招标模块 招标信息发布
function issueBidInvite(){
	
		var rows = $('#dg').datagrid('getSelections');
		var checkNum = rows.length;
		if(checkNum==0){
			$.messager.alert('错误','至少选择一项','error');
		 } else if(checkNum>1){
			$.messager.alert('错误','最多选择一项','error');
		 }else{
				var state =rows[0].state;
		
			if(state==0){
				$.messager.confirm('Confirm', '确定要发布吗?', function(r){
	if (r){
		var iInviteID = rows[0].inviteID; 
		 			 $.ajax({
        				url:"/mmy/issuebidinvite?iInviteID="+iInviteID,
        				type:"post",
        				data:"",
        				dataType:"text",
        				success:function (data) {
			 			
						if(data==2){
							$.messager.alert('信息','您的审请已提交，等待审核','info');
							$("#dg").datagrid("load");
						
						}else{
								$.messager.alert('错误','您的总团款低于2000元,不能发布','error');
						$("#dg").datagrid("load");
						} 	
        			},
       					 error:function () {
	$.messager.alert('错误','发布失败','error');
           			$("#dg").datagrid("load");
       				 }
    })
	}
});
			
			
		 }else if(state==1){
$.messager.alert('错误','您的总团款低于2000元,不能发布','error');
						$("#dg").datagrid("load");
}else if(state==4){
$.messager.alert('错误','审核未通过,不能发布','error');
						$("#dg").datagrid("load");
}
else{
		$.messager.alert('警告','等待发布或已发布成功','warning');
	$("#dg").datagrid("load");
}	
}

}

/*查看标书*/
//查看该招标的投标列表 hanxl 2020.11.19
function getBidTender(){
	
		var rows = $('#dg').datagrid('getSelections');
		var checkNum = rows.length;
		if(checkNum==0){
			$.messager.alert('错误','至少选择一项','error');
		 } else if(checkNum>1){
			$.messager.alert('错误','最多选择一项','error');
		 }else{
		
		var iInviteID = rows[0].inviteID; 
		 	location.href="/mmy/backtobidtenderindex?strInviteID="+iInviteID;
		 }
}

